<template>
  <div class="comment-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑路径导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ name: 'home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>评论管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 面包屑路径导航 -->
      </div>

      <!-- 列表 -->
      <el-table :data="articles" style="width: 100%" class="table-list">
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="total_comment_count" label="总评论数">
        </el-table-column>
        <el-table-column prop="fans_comment_count" label="粉丝评论数">
        </el-table-column>
        <el-table-column prop="name" label="状态">
          <template slot-scope="scope">
            {{ scope.row.comment_status ? "正常" : "关闭" }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="操作">
          <template slot-scope="scope">
            <el-switch
              :disabled="scope.row.statusDisabled"
              v-model="scope.row.comment_status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="onStatusChange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <!-- 
        绑定页码
        绑定每页大小
        current-page：指定当前高亮的页码，默认肯定是1，，但是要是写2的话，页码肯定是第二页 但是数据不是第二页
        page-sizes：可选的每页大小
      page-size：按照每页多大来进行分页，真正的每页大小
        layout:页码的布局
        加上.sync才会将每页大小同步到data数据当中
       -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size.sync="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  getArticles,
updateCommentStatus
  } from "@/api/article";
export default {
  name: "CommentIndex",
  data() {
    return {
      articles: [],//文章数据列表
      totalCount:0 ,//总数据条数
      pageSize: 10,
      page: 1 //当前激活的页码
    };
  },
  created() {
    this.loadArticles();
  },
  methods: {
    // 当每页大小改变 调用该事件
    handleSizeChange() {
      this.loadArticles(1)
    },
    //当页码改变就会触发事件 调用这个函数
    handleCurrentChange(page) {
      // 页码改变 加载指定页码数据
      this.loadArticles(page)
    },

    loadArticles ( page = 1) {
      // 让分页组件激活的页码和请求数据的页码保持一致
      this.page = page
      getArticles({
        response_type: "comment",
        page,
        per_page:this.pageSize
      }).then((res) => {
        const results = res.data.data.results;
        results.forEach(article => {
          article.statusDisabled = false
        })
        this.articles = results;
        this.totalCount = res.data.data.total_count
      });
    },
    onStatusChange (article) {
      // 禁用开关
      article.statusDisabled = true
      // 请求提交修改
      updateCommentStatus(article.id, article.comment_status).then(res => {
        // 启用按钮
        article.statusDisabled = false
        this.$message({
          type: 'success',
          message: article.comment_status ? '开启成功' : '关闭成功'
        })
      })
    }
  },
};
</script>

<style scoped lang="less">
.table-list {
  margin-bottom: 30px;
}
</style>